import 'dart:convert';
import 'dart:io';

void main() async {
  test1();
}

/// Splits a stream of consecutive strings into lines.
///
/// The input string is provided in smaller chunks through
/// the `source` stream.
Stream<String> lines(Stream<String> source) async* {
  // Stores any partial line from the previous chunk.
  var partial = '';
  // Wait until a new chunk is available, then process it.
  await for (final chunk in source) {
    var lines = chunk.split('\n');
    lines[0] = partial + lines[0]; // Prepend partial line.
    partial = lines.removeLast(); // Remove new partial line.
    for (final line in lines) {
      yield line; // Add lines to output stream.
    }
  }
  // Add final partial line to output stream, if any.
  if (partial.isNotEmpty) yield partial;
}

/// 测试从已有的Stream转换出新的流
void test1() async {
  await Stream.periodic(Duration(seconds: 1), (x) => x)
      .take(17)
      .where((event) => event.isEven)
      .expand((element) => [element])
      .map((event) => "value - ${event.toString()}")
      .forEach(print);
}

///
void test2() async{
  Stream<List<int>> content = File('someFile.txt').openRead();
  List<String> lines = await content
      .transform(utf8.decoder)
      .transform(const LineSplitter())
      .toList();
  print(lines.join("\n"));


}
/// 从Future转为Stream
Stream<T> streamFromFutures<T>(Iterable<Future<T>> futures) async* {
  for (final future in futures) {
    var result = await future;
    yield result;
  }
}

/// test3()
void testStreamController (){

}
